package com.farm.kaiping.controller;


import com.farm.kaiping.entity.SysTenant;
import com.farm.kaiping.po.SysTenantPO;
import com.farm.kaiping.service.SysTenantService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户表(SysTenant)表控制层
 *
 * @author zhuhui
 * @since 2024-12-01 16:14:32
 */
@RestController
@RequestMapping("/sysTenant")
@AllArgsConstructor
@Api(tags = "租户表控制器")
public class SysTenantController {

	private final SysTenantService sysTenantService;

	@ApiOperation(value = "分页查询租户表", notes = "权限：view_sysTenant_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysTenant_page')")
	@PostMapping("/page")
	public R<Page<SysTenant>> getSysTenantPage(@RequestBody SysTenantPO sysTenantPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(sysTenantPO);
		return R.ok(sysTenantService.getSysTenantPage(page, sysTenantPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询租户表列表", notes = "权限：view_sysTenant_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysTenant_list')")
	@PostMapping("/list")
	public R<List<SysTenant>> getSysTenantList(@RequestBody SysTenantPO sysTenantPO) {
		try{
			return R.ok(sysTenantService.getSysTenantList(sysTenantPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询租户表", notes = "权限 view_sysTenant_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysTenant_view')" )
	public R getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(sysTenantService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条租户表", notes = "权限：view_sysTenant_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysTenant_one')")
	@PostMapping("/one")
	public R<SysTenant> getSysTenant(@RequestBody SysTenantPO sysTenantPO) {
		try{
			return R.ok(sysTenantService.getSysTenant(sysTenantPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增租户表", notes = "权限：view_sysTenant_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysTenant_add')")
	@PostMapping("/add")
	public R saveSysTenant(@RequestBody SysTenantPO sysTenantPO) {
		try{
		    return R.ok(sysTenantService.saveSysTenant(sysTenantPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改租户表", notes = "权限：view_sysTenant_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysTenant_edit')")
	@PostMapping("/edit")
	public R updateSysTenant(@RequestBody SysTenantPO sysTenantPO) {
		try{
		    return R.ok(sysTenantService.updateSysTenant(sysTenantPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除租户表", notes = "权限：view_sysTenant_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysTenant_del')")
	@PostMapping("/del")
	public R removeSysTenant(@RequestBody SysTenantPO sysTenantPO) {
		try{
		    return R.ok(sysTenantService.removeSysTenant(sysTenantPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}
	
}

